package com.heather.exam.controller.Eclass;

import com.heather.exam.pojo.Result;
import com.heather.exam.pojo.vo.ClassScore;
import com.heather.exam.pojo.vo.UserInClassVO;
import com.heather.exam.service.ClassService;
import com.heather.exam.service.RegisterService;
import com.heather.exam.until.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ClassController {
    @Autowired
    ClassService classService;

    @PostMapping("/api/class/create")
    public Result addClass(String token,String name)
    {
        Integer role = (Integer) JwtUtils.parseJWT(token).get("user_role");
        Integer userId = (Integer) JwtUtils.parseJWT(token).get("user_id");
        if (role==1){
            return Result.error("您没有权限");
        }
        Integer integer = classService.addClass(name,userId);
        if (integer==0){
            return Result.error("添加失败");
        }
        return Result.success();
    }
//    加入班级
    @GetMapping("/api/class/apply")
    public Result apply(String token,String classId)
    {
        Integer userId = (Integer) JwtUtils.parseJWT(token).get("user_id");

        Integer integer = classService.joinClass(classId,userId);
        if (integer==0){
            return Result.error("已加入班级");
        }
        return Result.success("申请成功，等待批准");
    }
//    申请列表
    @GetMapping("/api/class/join_list")
    public Result joinList(String classId,String token)
    {
        Integer role = (Integer) JwtUtils.parseJWT(token).get("user_role");
        Integer userId = (Integer) JwtUtils.parseJWT(token).get("user_id");
        if (role==1){
            return Result.error("您没有权限");
        }
        List<UserInClassVO> joinList = classService.joinList(classId);
        return Result.success(joinList);
    }
    //    批准加入
    @GetMapping("/api/class/join")
    public Result join(String classId,String token)
    {
        Integer role = (Integer) JwtUtils.parseJWT(token).get("user_role");
        Integer userId = (Integer) JwtUtils.parseJWT(token).get("user_id");
        if (role==1){
            return Result.error("您没有权限");
        }
        List<UserInClassVO> joinList = classService.joinList(classId);
        return Result.success(joinList);
    }


    //得到所有成员
    @GetMapping("/api/class/getall")
    public Result getAll(String classId)
    {
        List<UserInClassVO> all = classService.getAll(classId);
        return Result.success(all);
    }
    @GetMapping("/api/class/kick")
    public Result getAll(String classId,String token,String examId)
    {
        Integer role = (Integer) JwtUtils.parseJWT(token).get("user_role");
        Integer userId = (Integer) JwtUtils.parseJWT(token).get("user_id");
        if (role==1){
            return Result.error("您没有权限");
        }
        List<ClassScore> score = classService.getScore(classId, examId);
        return Result.success(score);
    }


}
